#!/bin/bash

# 构建脚本

set -e

echo "Building Blockchain API Service..."

# 设置变量
APP_NAME="blockchain-api"
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
GO_VERSION=$(go version | awk '{print $3}')

# 构建标志
LDFLAGS="-X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME} -X main.GoVersion=${GO_VERSION}"

# 创建构建目录
mkdir -p build

# 构建不同平台的二进制文件
echo "Building for Linux..."
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o build/${APP_NAME}-linux-amd64 cmd/api/main.go

echo "Building for Windows..."
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o build/${APP_NAME}-windows-amd64.exe cmd/api/main.go

echo "Building for macOS..."
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o build/${APP_NAME}-darwin-amd64 cmd/api/main.go

echo "Building for macOS ARM64..."
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o build/${APP_NAME}-darwin-arm64 cmd/api/main.go

# 创建符号链接
ln -sf ${APP_NAME}-linux-amd64 build/${APP_NAME}

echo "Build completed successfully!"
echo "Binaries are available in the build/ directory:"
ls -la build/
